package com.sz.jinzheng.controller;

import com.sz.jinzheng.client.UserCilent;
import com.sz.jinzheng.model.PageBean;
import com.sz.jinzheng.model.Result;
import com.sz.jinzheng.model.Task;
import com.sz.jinzheng.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/manager")
public class TaskController {
    @Autowired
    private TaskService taskService;
    @Autowired
    private UserCilent userCilent;

    //多条件查询任务
    @RequestMapping("/getTaskByExam")
    public Result getTaskByExam(@RequestBody Task task, int index, int size) {
        PageBean<Task> taskByExam = taskService.getTaskByExam(task, index, size);
        return new Result(200, "成功", taskByExam);
    }

    /**
     * 领取任务后在审核人员信息表中增加审批记录
     *
     * @param task
     * @return
     */
    @RequestMapping("/pickTask")
    public Result pickTask(@RequestBody Task task) {
        //通过token拿到uid
        Integer uid = userCilent.getUid(task.getToken());
        task.setUserId(uid);
        log.info("task=" + task);
        task.setStatus(7);//业务经理领取任务后，更改领取任务状态为7

        int i = taskService.updateTaskStatus(task);
        if (i > 0) {
            return new Result(200, "领取成功", null);
        }
        return new Result(409, "领取失败请稍后重试", null);
    }

    /**
     * 查询自己的待完成的任务
     *
     * @param task
     * @param index
     * @param size
     * @return
     */
    @RequestMapping("/getMyCompletingTask")
    public Result getMyCompletingTask(@RequestBody Task task, int index, int size) {
        //通过token拿到uid
        Integer uid = userCilent.getUid(task.getToken());
        task.setUserId(uid);
        log.info("task=" + task);

        log.info(task.toString());
        log.info(String.valueOf(index));
        PageBean<Task> taskPageBean = taskService.getMyCompletingTask(task, index, size);
        return new Result(200, "查询成功", taskPageBean);
    }
}
